﻿using System;

namespace xlstool
{
    using NPOI.HSSF.Record;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    public class Excel2003Row
    {
        internal IRow row { get; set; }
        internal Excel2003Sheet sheet { get; set; }
        internal Excel2003Row(IRow row, Excel2003Sheet sheet)
        {
            this.row = row;
            this.sheet = sheet;
        }
        public int RowNo { get { return row.RowNum + 1; } }
        public Excel2003Row Next()
        {
            var _row = sheet.sheet.GetRow(row.RowNum + 1);
            if (_row == null) _row = sheet.sheet.CreateRow(row.RowNum + 1);
            this.row  = _row;
            return this;
        }
        public Excel2003Cell ToCell(int index)
        {
            index--;
            if (index < 0) throw new ArgumentException();

            var cell = row.GetCell(index);
            if (cell == null) cell = row.CreateCell(index);

            return new Excel2003Cell(cell, this);
        }
        public Excel2003Cell ToCell(string cellname)
        {
            return ToCell(Excel2003Cell.to_cellno(cellname));
        }
        public IEnumerable<Excel2003Cell> LoopCell()
        {
            int firstCellNum = row.FirstCellNum;
            int lastCellNum = row.LastCellNum;
            for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++)
            {
                ICell cell = row.GetCell(cellNum);
                if (cell == null) continue;
                yield return new Excel2003Cell(cell, this);
            }
        }
        public void CopyTo(Excel2003Row row)
        {
            foreach (var cell in LoopCell()) cell.CopyTo(row.ToCell(cell.CellNo));
        }
        public bool Hidden { get { return this.row.ZeroHeight; } set { this.row.ZeroHeight = value; } }
        public int Height { get { return (int)this.row.HeightInPoints; } set { this.row.HeightInPoints = value; } }
    }
}
